var meterInfo = [];
var meterRecord = [];
var changeIndex = undefined;
var flagDate = undefined;
var chargingFixedPriceNameList = [];
var chargingTimePriceNameList = [];

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	$ajaxList('searchBalanceInitSetting', getBalanceInitSetting);
});
function getBalanceInitSetting(data){
	var balanceInitSettingData = data.balanceInitSettingList[0]
	$("#BalanceChargingPriceType").val(balanceInitSettingData.balanceChargingPriceType);
	var balanceStartMonth = balanceInitSettingData.balanceStartMonth
	changeYear(0, balanceStartMonth);
	showMonth(getSystemTime().getMonth() + 1);
	setTimeout(hideLoading, 500);
}
function initChargingPriceName(type) {
	var selectLength = $("#EffectiveDTTM option").length;
	if (selectLength < 1) {
		getChargingPriceName(type);
	} else {
		search(true);
	}
}
function getChargingPriceName(type) {
	if (type == '1') {
		if (chargingFixedPriceNameList.length > 1) {
			getChargingFixedPriceName(chargingFixedPriceNameList);
		} else {
			$ajaxList('chargingFixedPriceName', getChargingFixedPriceName);
		}
	} else if (type == '2') {
		if (chargingTimePriceNameList.length > 1) {
			getChargingTimePriceName(chargingTimePriceNameList);
		} else {
			$ajaxList('chargingTimePriceName', getChargingTimePriceName);
		}
	}
}
function getChargingFixedPriceName(data) {
	chargingFixedPriceNameList = data;
	$("#ChargingPrice").empty();
	$("#EffectiveDTTM").empty();
//	$("#EffectiveDTTM").append("<option value='' selected='selected'>请选择</option>");
	var length = data.length;
	if (length == 0) {
		$("#EffectiveDTTM").attr('disabled', 'disabled');
		$("#save").attr('disabled', 'disabled');
		$("#totalCost").text('0.00');
		$("#totalTradeKWH").text('0');
		$("#costBalanceBillThead").empty();
		$("#costBalanceBillTbody").empty();
		showMessge(messageContent["Balance.ChargingPriceNotFound"]);
		return;
	}
	var chargingPrice = undefined;
	for (var i = 0; i < length; i++) {
		chargingPrice = '固：' + data[i].chargingPrice + '元';
		if (data[i].enabled) {
			$("#EffectiveDTTM").append("<option value='" + chargingPrice + "' selected='selected'>" + date2String(data[i].effectiveDTTM) + "</option>");
			getChargingPrice(chargingPrice, i);
		} else {
			$("#EffectiveDTTM").append("<option value='" + chargingPrice + "'>" + date2String(data[i].effectiveDTTM) + "</option>");
		}
	}
	if (!$("#ChargingPrice").text()) {
		getChargingPrice('固：' + data[0].chargingPrice + '元');
		$("#ChargingUnitPrice").val(data[0].chargingPrice);
	}
	search();
}
function getChargingTimePriceName(data) {
	chargingTimePriceNameList = data;
	$("#ChargingPrice").empty();
	$("#EffectiveDTTM").empty();
//	$("#EffectiveDTTM").append("<option value='' selected='selected'>请选择</option>");
	var length = data.length;
	if (length == 0) {
		showMessge(messageContent["Balance.ChargingPriceNotFound"]);
		$("#EffectiveDTTM").attr('disabled', 'disabled');
		$("#save").attr('disabled', 'disabled');
		$("#totalCost").text('0.00');
		$("#totalTradeKWH").text('0');
		$("#costBalanceBillThead").empty();
		$("#costBalanceBillTbody").empty();
		return;
	}
	var chargingPrice = undefined;
	for (var i = 0; i < length; i++) {
		chargingPrice = '尖:' + data[i].sharpChargingPrice +'元， 峰:' + data[i].peakChargingPrice + '元， 平:' + data[i].flatChargingPrice + '元， 谷:' + data[i].valleyChargingPrice + '元';
		if (data[i].enabled) {
			$("#EffectiveDTTM").append("<option value='" + chargingPrice + "' selected='selected'>" + date2String(data[i].effectiveDTTM) + "</option>");
			getChargingPrice(chargingPrice, i);
		} else {
			$("#EffectiveDTTM").append("<option value='" + chargingPrice + "'>" + date2String(data[i].effectiveDTTM) + "</option>");
		}
	}
	if (!$("#ChargingPrice").text()) {
		getChargingPrice('尖:' + data[0].sharpChargingPrice +'元， 峰:' + data[0].peakChargingPrice + '元， 平:' + data[0].flatChargingPrice + '元， 谷:' + data[0].valleyChargingPrice + '元');
	}
	search();
}
function getChargingPrice(chargingPrice, index) {
	$("#ChargingPrice").text(chargingPrice);
	$("#chargingPriceIndex").val(index);
	var index = undefined;
	var totalCost = 0;
	var balanceChargingPriceType = $("#BalanceChargingPriceType").val();
	if (balanceChargingPriceType == '1') {
		$("div[id*='totalActiveKWH']").each(function(i, item) {
			if (item.id){
				index = item.id.substr('totalActiveKWH'.length);
				if ($("#" + ("totalActiveKWH" + index)).text()) {
					$("#" + ("costSubtotal" + index)).text((parseInt($("#" + ("totalActiveKWH" + index)).text() || 0, 10) * Number((chargingFixedPriceNameList && chargingFixedPriceNameList[$("#chargingPriceIndex").val()].chargingPrice || 0))).toFixed(4));
					totalCost = totalCost + Number($("#" + ("costSubtotal" + index)).text());
				}
			}
		})
	} else if (balanceChargingPriceType == '2') {
		var length = meterInfo.length;
		var subTotalCost = undefined;
		for (var i=0; i<length; i++) {
			subTotalCost = 0;
			if ($("#" + ("sharpActiveKWH" + i)).text()) {
				subTotalCost = subTotalCost + parseInt($("#" + ("sharpActiveKWH" + i)).text() || 0, 10) * Number((chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].sharpChargingPrice || 0));
			}
			if ($("#" + ("peakActiveKWH" + i)).text()) {
				subTotalCost = subTotalCost + parseInt($("#" + ("peakActiveKWH" + i)).text() || 0, 10) * Number((chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].peakChargingPrice || 0));
			}
			if ($("#" + ("flatActiveKWH" + i)).text()) {
				subTotalCost = subTotalCost + parseInt($("#" + ("flatActiveKWH" + i)).text() || 0, 10) * Number((chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].flatChargingPrice || 0));
			}
			if ($("#" + ("valleyActiveKWH" + i)).text()) {
				subTotalCost = subTotalCost + parseInt($("#" + ("valleyActiveKWH" + i)).text() || 0, 10) * Number((chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].valleyChargingPrice || 0));
			}
			$("#" + ("costTimeSubtotal" + i)).text(Number(subTotalCost).toFixed(4));
			totalCost = totalCost + subTotalCost;
		}
	}
	if (totalCost != 0) {
		$("#totalCost").text(Number(totalCost).toFixed(2));
	}
}
function setCheckMeterTimeSelect(ID, index, chargingStationID){
	var select = '<select id="' + (ID + index) + '" style="width: 150px;" class="form-control" ';
	if (ID == 'CheckMeterLastTime') {
		select = select + 'onchange="getLastMeterRecord(this.options[this.options.selectedIndex].value, ' + index + ')">';
	} else if (ID == 'CheckMeterThisTime') {
		select = select + 'onchange="getThisMeterRecord(this.options[this.options.selectedIndex].value, ' + index + ')">';
	}
	var length = meterRecord.length;
	var meterDate = undefined;
	var tempChargingStationID = undefined;
	for (var i=0; i<length; i++) {
		if (chargingStationID == meterRecord[i].chargingStationID) {
			tempChargingStationID = meterRecord[i].chargingStationID;
			if (meterDate != meterRecord[i].meterDate) {
				meterDate = meterRecord[i].meterDate;
				if ((ID == 'CheckMeterLastTime' && i==0) || (ID == 'CheckMeterThisTime')) {
					select = select + "<option value='" + i + "' selected='selected'>" + meterRecord[i].meterDate + "</option>";
				} else {
					select = select + "<option value='" + i + "'>" + meterRecord[i].meterDate + "</option>";
				}
			}
		} else if (tempChargingStationID) {
			break;
		}
	}
	select = select + "</select>";
	
	return select;
}
function getLastMeterRecord(value, index) {
	var meterRecordData = undefined;
	var meterInfoData = undefined;
	var meterDate = meterRecord[value] && meterRecord[value].meterDate;
	var chargingStationID = meterInfo[index] && meterInfo[index].chargingStationID;
	var j = index;
	var length = meterRecord.length;
	var balanceChargingPriceType = $("#BalanceChargingPriceType").val();
	if (balanceChargingPriceType == '1') {
		if ($("#" + ("totalActiveKWH" + index)).text()) {
			$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) - Number($("#" + ("totalActiveKWH" + index)).text())).toFixed(0));
		}
		if ($("#" + ("costSubtotal" + index)).text()) {
			$("#totalCost").text((Number($("#totalCost").text()) - Number($("#" + ("costSubtotal" + index)).text())).toFixed(2));
		}
		$("#" + ("totalActiveKWH" + index)).text('');
		$("#" + ("costSubtotal" + index)).text('');
	}
	for (var i=value; i<length; i++) {
		meterRecordData = meterRecord[i];
		meterInfoData = meterInfo[j];
		if (meterInfoData && chargingStationID == meterInfoData.chargingStationID){
			if (meterDate == meterRecordData.meterDate && meterRecordData.chargingStationID == meterInfoData.chargingStationID && meterRecordData.meterCode == meterInfoData.meterCode) {
				if (balanceChargingPriceType == '1') {
					$("#" + ("checkMeterLastNo" + j)).text(meterRecordData.powerTotal);
					if ($("#" + ("checkMeterThisNo" + j)).text()) {
						$("#" + ("activeKWH" + j)).text(((Number($("#" + ("checkMeterThisNo" + j)).text()) - Number($("#" + ("checkMeterLastNo" + j)).text())).toFixed(2) * parseInt($("#" + ("checkMeterRate" + j)).text() || 0, 10)).toFixed(0));
						$("#" + ("totalActiveKWH" + index)).text((parseInt($("#" + ("totalActiveKWH" + index)).text() || 0, 10) + parseInt($("#" + ("activeKWH" + j)).text() || 0, 10)).toFixed(0));
//					$("#" + ("costSubtotal" + index)).text((parseInt($("#" + ("totalActiveKWH" + index)).text() || 0, 10) * Number($("#" + ("chargingFixedPrice" + index)).text())).toFixed(4));
						$("#" + ("costSubtotal" + index)).text((parseInt($("#" + ("totalActiveKWH" + index)).text() || 0, 10) * Number((chargingFixedPriceNameList && chargingFixedPriceNameList[$("#chargingPriceIndex").val()].chargingPrice || 0))).toFixed(4));
					}
				} else if (balanceChargingPriceType == '2') {
					if ($("#" + ("costTimeSubtotal" + j)).text()) {
						$("#totalCost").text((Number($("#totalCost").text()) - Number($("#" + ("costTimeSubtotal" + j)).text())).toFixed(2));
					}
					if ($("#" + ("sharpActiveKWH" + j)).text()) {
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) - Number($("#" + ("sharpActiveKWH" + index)).text())).toFixed(0));
					}
					if ($("#" + ("peakActiveKWH" + j)).text()) {
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) - Number($("#" + ("peakActiveKWH" + index)).text())).toFixed(0));
					}
					if ($("#" + ("flatActiveKWH" + j)).text()) {
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) - Number($("#" + ("flatActiveKWH" + index)).text())).toFixed(0));
					}
					if ($("#" + ("valleyActiveKWH" + j)).text()) {
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) - Number($("#" + ("valleyActiveKWH" + index)).text())).toFixed(0));
					}
					$("#" + ("costTimeSubtotal" + j)).text('');
					$("#" + ("sharpActiveKWH" + j)).text('');
					$("#" + ("peakActiveKWH" + j)).text('');
					$("#" + ("flatActiveKWH" + j)).text('');
					$("#" + ("valleyActiveKWH" + j)).text('');
					$("#" + ("sharpMeterLastNo" + j)).text(meterRecordData.powerAiguille);
					$("#" + ("peakMeterLastNo" + j)).text(meterRecordData.powerPeak);
					$("#" + ("flatMeterLastNo" + j)).text(meterRecordData.powerFlat);
					$("#" + ("valleyMeterLastNo" + j)).text(meterRecordData.powerGrain);
					if ($("#" + ("sharpMeterThisNo" + j)).text()) {
						$("#" + ("sharpActiveKWH" + j)).text(((Number($("#" + ("sharpMeterThisNo" + j)).text()) - Number($("#" + ("sharpMeterLastNo" + j)).text())).toFixed(2) * parseInt($("#" + ("checkMeterRate" + j)).text() || 0, 10)).toFixed(0));
						$("#" + ("costTimeSubtotal" + j)).text((Number($("#" + ("costTimeSubtotal" + j)).text() || 0) + parseInt($("#" + ("sharpActiveKWH" + index)).text() || 0, 10) * Number((chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].sharpChargingPrice || 0))).toFixed(4));
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) + Number($("#" + ("sharpActiveKWH" + index)).text())).toFixed(0));
					}
					if ($("#" + ("peakMeterThisNo" + j)).text()) {
						$("#" + ("peakActiveKWH" + j)).text(((Number($("#" + ("peakMeterThisNo" + j)).text()) - Number($("#" + ("peakMeterLastNo" + j)).text())).toFixed(2) * parseInt($("#" + ("checkMeterRate" + j)).text() || 0, 10)).toFixed(0));
						$("#" + ("costTimeSubtotal" + j)).text((Number($("#" + ("costTimeSubtotal" + j)).text() || 0) + parseInt($("#" + ("peakActiveKWH" + index)).text() || 0, 10) * Number((chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].peakChargingPrice || 0))).toFixed(4));
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) + Number($("#" + ("peakActiveKWH" + index)).text())).toFixed(0));
					}
					if ($("#" + ("flatMeterThisNo" + j)).text()) {
						$("#" + ("flatActiveKWH" + j)).text(((Number($("#" + ("flatMeterThisNo" + j)).text()) - Number($("#" + ("flatMeterLastNo" + j)).text())).toFixed(2) * parseInt($("#" + ("checkMeterRate" + j)).text() || 0, 10)).toFixed(0));
						$("#" + ("costTimeSubtotal" + j)).text((Number($("#" + ("costTimeSubtotal" + j)).text() || 0) + parseInt($("#" + ("flatActiveKWH" + index)).text() || 0, 10) * Number((chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].flatChargingPrice || 0))).toFixed(4));
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) + Number($("#" + ("flatActiveKWH" + index)).text())).toFixed(0));
					}
					if ($("#" + ("valleyMeterThisNo" + j)).text()) {
						$("#" + ("valleyActiveKWH" + j)).text(((Number($("#" + ("valleyMeterThisNo" + j)).text()) - Number($("#" + ("valleyMeterLastNo" + j)).text())).toFixed(2) * parseInt($("#" + ("checkMeterRate" + j)).text() || 0, 10)).toFixed(0));
						$("#" + ("costTimeSubtotal" + j)).text((Number($("#" + ("costTimeSubtotal" + j)).text() || 0) + parseInt($("#" + ("valleyActiveKWH" + index)).text() || 0, 10) * Number((chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].valleyChargingPrice || 0))).toFixed(4));
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) + Number($("#" + ("valleyActiveKWH" + index)).text())).toFixed(0));
					}
					$("#totalCost").text((Number($("#totalCost").text()) + Number($("#" + ("costTimeSubtotal" + j)).text())).toFixed(2));
				}
			} else {
				if (balanceChargingPriceType == '1') {
					$("#" + ("checkMeterLastNo" + j)).text('');
					$("#" + ("activeKWH" + j)).text('');
				} else if (balanceChargingPriceType == '2') {
					$("#" + ("sharpMeterLastNo" + j)).text('');
					$("#" + ("peakMeterLastNo" + j)).text('');
					$("#" + ("flatMeterLastNo" + j)).text('');
					$("#" + ("valleyMeterLastNo" + j)).text('');
					$("#" + ("sharpActiveKWH" + j)).text('');
					$("#" + ("peakActiveKWH" + j)).text('');
					$("#" + ("flatActiveKWH" + j)).text('');
					$("#" + ("valleyActiveKWH" + j)).text('');
				}
			}
			j++;
		} else {
			break;
		}
	}
	if (balanceChargingPriceType == '1') {
		$("#totalCost").text((Number($("#totalCost").text()) + Number($("#" + ("costSubtotal" + index)).text())).toFixed(2));
		$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) + Number($("#" + ("totalActiveKWH" + index)).text())).toFixed(0));
	}
}
function getThisMeterRecord(value, index) {
	var meterRecordData = undefined;
	var meterInfoData = undefined;
	var meterDate = meterRecord[value] && meterRecord[value].meterDate;
	var chargingStationID = meterInfo[index] && meterInfo[index].chargingStationID;
	var j = index;
	var length = meterRecord.length;
	var balanceChargingPriceType = $("#BalanceChargingPriceType").val();
	if (balanceChargingPriceType == '1') {
		if ($("#" + ("totalActiveKWH" + index)).text()) {
			$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) - Number($("#" + ("totalActiveKWH" + index)).text())).toFixed(0));
		}
		if ($("#" + ("costSubtotal" + index)).text()) {
			$("#totalCost").text((Number($("#totalCost").text()) - Number($("#" + ("costSubtotal" + index)).text())).toFixed(2));
		}
		$("#" + ("totalActiveKWH" + index)).text('');
		$("#" + ("costSubtotal" + index)).text('');
	}
	for (var i=value; i<length; i++) {
		meterRecordData = meterRecord[i];
		meterInfoData = meterInfo[j];
		if (meterInfoData && chargingStationID == meterInfoData.chargingStationID){
			if (meterDate == meterRecordData.meterDate && meterRecordData.chargingStationID == meterInfoData.chargingStationID && meterRecordData.meterCode == meterInfoData.meterCode) {
				if (balanceChargingPriceType == '1') {
					$("#" + ("checkMeterThisNo" + j)).text(meterRecordData.powerTotal);
					if ($("#" + ("checkMeterLastNo" + j)).text()) {
						$("#" + ("activeKWH" + j)).text(((Number($("#" + ("checkMeterThisNo" + j)).text()) - Number($("#" + ("checkMeterLastNo" + j)).text())).toFixed(2) * parseInt($("#" + ("checkMeterRate" + j)).text() || 0, 10)).toFixed(0));
						$("#" + ("totalActiveKWH" + index)).text((parseInt($("#" + ("totalActiveKWH" + index)).text() || 0, 10) + parseInt($("#" + ("activeKWH" + j)).text() || 0, 10)).toFixed(0));
	//					$("#" + ("costSubtotal" + index)).text((parseInt($("#" + ("totalActiveKWH" + index)).text() || 0, 10) * Number($("#" + ("chargingFixedPrice" + index)).text())).toFixed(4));
						$("#" + ("costSubtotal" + index)).text((parseInt($("#" + ("totalActiveKWH" + index)).text() || 0, 10) * Number((chargingFixedPriceNameList && chargingFixedPriceNameList[$("#chargingPriceIndex").val()].chargingPrice || 0))).toFixed(4));
					}
				} else if (balanceChargingPriceType == '2') {
					if ($("#" + ("costTimeSubtotal" + j)).text()) {
						$("#totalCost").text((Number($("#totalCost").text()) - Number($("#" + ("costTimeSubtotal" + j)).text())).toFixed(2));
					}
					if ($("#" + ("sharpActiveKWH" + j)).text()) {
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) - Number($("#" + ("sharpActiveKWH" + index)).text())).toFixed(0));
					}
					if ($("#" + ("peakActiveKWH" + j)).text()) {
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) - Number($("#" + ("peakActiveKWH" + index)).text())).toFixed(0));
					}
					if ($("#" + ("flatActiveKWH" + j)).text()) {
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) - Number($("#" + ("flatActiveKWH" + index)).text())).toFixed(0));
					}
					if ($("#" + ("valleyActiveKWH" + j)).text()) {
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) - Number($("#" + ("valleyActiveKWH" + index)).text())).toFixed(0));
					}
					$("#" + ("costTimeSubtotal" + j)).text('');
					$("#" + ("sharpActiveKWH" + j)).text('');
					$("#" + ("peakActiveKWH" + j)).text('');
					$("#" + ("flatActiveKWH" + j)).text('');
					$("#" + ("valleyActiveKWH" + j)).text('');
					$("#" + ("sharpMeterThisNo" + j)).text(meterRecordData.powerAiguille);
					$("#" + ("peakMeterThisNo" + j)).text(meterRecordData.powerPeak);
					$("#" + ("flatMeterThisNo" + j)).text(meterRecordData.powerFlat);
					$("#" + ("valleyMeterThisNo" + j)).text(meterRecordData.powerGrain);
					if ($("#" + ("sharpMeterLastNo" + j)).text()) {
						$("#" + ("sharpActiveKWH" + j)).text(((Number($("#" + ("sharpMeterThisNo" + j)).text()) - Number($("#" + ("sharpMeterLastNo" + j)).text())).toFixed(2) * parseInt($("#" + ("checkMeterRate" + j)).text() || 0, 10)).toFixed(0));
						$("#" + ("costTimeSubtotal" + j)).text((Number($("#" + ("costTimeSubtotal" + j)).text() || 0) + parseInt($("#" + ("sharpActiveKWH" + index)).text() || 0, 10) * Number((chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].sharpChargingPrice || 0))).toFixed(4));
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) + Number($("#" + ("sharpActiveKWH" + index)).text())).toFixed(0));
					}
					if ($("#" + ("peakMeterLastNo" + j)).text()) {
						$("#" + ("peakActiveKWH" + j)).text(((Number($("#" + ("peakMeterThisNo" + j)).text()) - Number($("#" + ("peakMeterLastNo" + j)).text())).toFixed(2) * parseInt($("#" + ("checkMeterRate" + j)).text() || 0, 10)).toFixed(0));
						$("#" + ("costTimeSubtotal" + j)).text((Number($("#" + ("costTimeSubtotal" + j)).text() || 0) + parseInt($("#" + ("peakActiveKWH" + index)).text() || 0, 10) * Number((chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].peakChargingPrice || 0))).toFixed(4));
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) + Number($("#" + ("peakActiveKWH" + index)).text())).toFixed(0));
					}
					if ($("#" + ("flatMeterLastNo" + j)).text()) {
						$("#" + ("flatActiveKWH" + j)).text(((Number($("#" + ("flatMeterThisNo" + j)).text()) - Number($("#" + ("flatMeterLastNo" + j)).text())).toFixed(2) * parseInt($("#" + ("checkMeterRate" + j)).text() || 0, 10)).toFixed(0));
						$("#" + ("costTimeSubtotal" + j)).text((Number($("#" + ("costTimeSubtotal" + j)).text() || 0) + parseInt($("#" + ("flatActiveKWH" + index)).text() || 0, 10) * Number((chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].flatChargingPrice || 0))).toFixed(4));
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) + Number($("#" + ("flatActiveKWH" + index)).text())).toFixed(0));
					}
					if ($("#" + ("valleyMeterLastNo" + j)).text()) {
						$("#" + ("valleyActiveKWH" + j)).text(((Number($("#" + ("valleyMeterThisNo" + j)).text()) - Number($("#" + ("valleyMeterLastNo" + j)).text())).toFixed(2) * parseInt($("#" + ("checkMeterRate" + j)).text() || 0, 10)).toFixed(0));
						$("#" + ("costTimeSubtotal" + j)).text((Number($("#" + ("costTimeSubtotal" + j)).text() || 0) + parseInt($("#" + ("valleyActiveKWH" + index)).text() || 0, 10) * Number((chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].valleyChargingPrice || 0))).toFixed(4));
						$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) + Number($("#" + ("valleyActiveKWH" + index)).text())).toFixed(0));
					}
					$("#totalCost").text((Number($("#totalCost").text()) + Number($("#" + ("costTimeSubtotal" + j)).text())).toFixed(2));
				}
			} else {
				if (balanceChargingPriceType == '1') {
					$("#" + ("checkMeterThisNo" + j)).text('');
					$("#" + ("activeKWH" + j)).text('');
				} else if (balanceChargingPriceType == '2') {
					$("#" + ("sharpMeterThisNo" + j)).text('');
					$("#" + ("peakMeterThisNo" + j)).text('');
					$("#" + ("flatMeterThisNo" + j)).text('');
					$("#" + ("valleyMeterThisNo" + j)).text('');
					$("#" + ("sharpActiveKWH" + j)).text('');
					$("#" + ("peakActiveKWH" + j)).text('');
					$("#" + ("flatActiveKWH" + j)).text('');
					$("#" + ("valleyActiveKWH" + j)).text('');
				}
			}
			j++;
		} else {
			break;
		}
	}
	if (balanceChargingPriceType == '1') {
		$("#totalCost").text((Number($("#totalCost").text()) + Number($("#" + ("costSubtotal" + index)).text())).toFixed(2));
		$("#totalTradeKWH").text((Number($("#totalTradeKWH").text()) + Number($("#" + ("totalActiveKWH" + index)).text())).toFixed(0));
	}
}
function search(research){
	$("#costBalanceBillThead").show();
	$("#errorMessage").hide();
	$("#errorMessage").empty();
	if (meterInfo.length == 0 || meterRecord.length == 0 || research) {
		$ajax('searchMeterInfo', 'costBalanceBillSearch', searchMeterInfo);
	} else {
		var data = {};
		data.meterInfo = meterInfo;
		data.meterRecord = meterRecord;
//		data.exist = true;
		searchMeterInfo(data);
	}
}
function searchMeterInfo(data){
	$("#totalCost").text('0.00');
	$("#totalTradeKWH").text('0');
	$("#costBalanceBillThead").empty();
	var costBalanceBillTbody = $("#costBalanceBillTbody");
	costBalanceBillTbody.empty();
	var balanceChargingPriceType = $("#BalanceChargingPriceType").val();
	if (balanceChargingPriceType == '1') {
		createTableHead("costBalanceBillThead", costBalanceBillTable);
	} else if (balanceChargingPriceType == '2') {
		createTableHead("costBalanceBillThead", costBalanceBillTimeTable);
	}
	if (!$("#chargingPriceIndex").val()) {
		$("#chargingPriceIndex").val('0');
		$("#ChargingPrice").text($("#EffectiveDTTM").val());
	}
	meterInfo = data && data.meterInfo || [];
	meterRecord = data && data.meterRecord || [];
//	var exist = data.exist;
	
	var meterInfoLength = meterInfo.length;
	var meterRecordLength = meterRecord.length;
	if (meterRecordLength > 0) {
		if (meterInfoLength > 0) {
			var chargingStationID = undefined;
			var rowspanCount = undefined;
			var number = 0;
			var checkMeterLastTimeSelect = undefined;
			var checkMeterThisTimeSelect = undefined;
			var meterDate = undefined;
			for(var i=0; i<meterInfoLength + 1; i++){
				if (i == meterInfoLength || chargingStationID != meterInfo[i].chargingStationID) {
					chargingStationID = meterInfo[i] && meterInfo[i].chargingStationID;
					if (rowspanCount) {
						for (var j=i - rowspanCount; j<i; j++) {
							$tr = $('<tr>').appendTo(costBalanceBillTbody);
							if (j==i - rowspanCount) {
								$('<td rowspan="'+ rowspanCount + '">').html(number).appendTo($tr);
								$('<td rowspan="'+ rowspanCount + '">').html(meterInfo[i-1].chargingStationName).appendTo($tr);
								$('<td rowspan="'+ rowspanCount + '">').html(setCheckMeterTimeSelect("CheckMeterLastTime", j, meterInfo[i-1].chargingStationID)).appendTo($tr);
								$('<td rowspan="'+ rowspanCount + '">').html(setCheckMeterTimeSelect("CheckMeterThisTime", j, meterInfo[i-1].chargingStationID)).appendTo($tr);
							}
							$('<td>').html(meterInfo[j].meterCode).appendTo($tr);
							$('<td>').html("<div id='" + ("checkMeterRate" + j) + "'>" + meterInfo[j].meterRate + "</div>").appendTo($tr);
							if (balanceChargingPriceType == '1') {
								$('<td>').html("<div id='" + ("checkMeterLastNo" + j) + "'></div>").appendTo($tr);
								$('<td>').html("<div id='" + ("checkMeterThisNo" + j) + "'></div>").appendTo($tr);
								$('<td>').html("<div id='" + ("activeKWH" + j) + "'></div>").appendTo($tr);
								if (j==i - rowspanCount) {
									$('<td rowspan="'+ rowspanCount + '">').html("<div id='" + ("totalActiveKWH" + j) + "'>" + "</div>").appendTo($tr);
//									$('<td rowspan="'+ rowspanCount + '">').html("<div id='" + ("chargingFixedPrice" + j) + "'>" + (chargingFixedPriceNameList && chargingFixedPriceNameList[$("#chargingPriceIndex").val()].chargingPrice) + "</div>").appendTo($tr);
									$('<td rowspan="'+ rowspanCount + '">').html("<div id='" + ("costSubtotal" + j) + "'></div>").appendTo($tr);
								}
							} else if (balanceChargingPriceType == '2') {
								$('<td>').html("<div id='" + ("sharpMeterLastNo" + j) + "'></div>").appendTo($tr);
								$('<td>').html("<div id='" + ("peakMeterLastNo" + j) + "'></div>").appendTo($tr);
								$('<td>').html("<div id='" + ("flatMeterLastNo" + j) + "'></div>").appendTo($tr);
								$('<td>').html("<div id='" + ("valleyMeterLastNo" + j) + "'></div>").appendTo($tr);
								$('<td>').html("<div id='" + ("sharpMeterThisNo" + j) + "'></div>").appendTo($tr);
								$('<td>').html("<div id='" + ("peakMeterThisNo" + j) + "'></div>").appendTo($tr);
								$('<td>').html("<div id='" + ("flatMeterThisNo" + j) + "'></div>").appendTo($tr);
								$('<td>').html("<div id='" + ("valleyMeterThisNo" + j) + "'></div>").appendTo($tr);
								$('<td>').html("<div id='" + ("sharpActiveKWH" + j) + "'></div>").appendTo($tr);
//								$('<td>').html("<div id='" + ("sharpChargingPrice" + j) + "'></div>").appendTo($tr);
								$('<td>').html("<div id='" + ("peakActiveKWH" + j) + "'></div>").appendTo($tr);
//								$('<td>').html("<div id='" + ("peakChargingPrice" + j) + "'></div>").appendTo($tr);
								$('<td>').html("<div id='" + ("flatActiveKWH" + j) + "'></div>").appendTo($tr);
//								$('<td>').html("<div id='" + ("flatChargingPrice" + j) + "'></div>").appendTo($tr);
								$('<td>').html("<div id='" + ("valleyActiveKWH" + j) + "'></div>").appendTo($tr);
//								$('<td>').html("<div id='" + ("valleyChargingPrice" + j) + "'></div>").appendTo($tr);
								$('<td>').html("<div id='" + ("costTimeSubtotal" + j) + "'></div>").appendTo($tr);
							}
						}
						getLastMeterRecord($("#" + ("CheckMeterLastTime" + (i - rowspanCount))).val(), i - rowspanCount);
						getThisMeterRecord($("#" + ("CheckMeterThisTime" + (i - rowspanCount))).val(), i - rowspanCount);
					}
					rowspanCount = 1;
					number++;
				} else {
					rowspanCount++;
				}
			}
			$("#save").attr('disabled', false);
		} else {
			$("#costBalanceBillThead").hide();
			$("#errorMessage").show();
			$("#errorMessage").html(messageContent["Balance.MeterInfoNotFound"]);
//			showMessge(messageContent["Balance.MeterInfoNotFound"]);
			$("#save").attr('disabled', 'disabled');
		}
	} else {
		$("#costBalanceBillThead").hide();
		$("#errorMessage").show();
		$("#errorMessage").html(messageContent["Balance.MonthMeterRecordNotFound"]);
//		showMessge(messageContent["Balance.MonthMeterRecordNotFound"]);
		$("#save").attr('disabled', 'disabled');
	}
	setTimeout(hideLoading, 500);
}
function showMonth(index){
	$('.activeMonthLi').removeClass("activeMonthLi");
	$("#li"+ index).addClass("activeMonthLi");
	//执行查询时间起止方法
	var year = $("#showYear").html().replace(/[^0-9]/ig,"");
	index = index >= 10 ? index : "0" + index;
	$("#balanceMonth").val(year + index);
	initChargingPriceName($("#BalanceChargingPriceType").val());
}
function saveBillInfo(){
	if (!$("#BalanceBillName").val()) {
		$("#BalanceBillName").tips({
			side : 2,
			msg : "请输入电力结算单名称",
			bg : '#AE81FF',
			time : 1
		});
		$("#BalanceBillName").focus();
		return;
	} else if (!$("#BalanceChargingPriceType").val()) {
		$("#BalanceChargingPriceType").tips({
			side : 2,
			msg : "请选择电价类型",
			bg : '#AE81FF',
			time : 1
		});
		$("#BalanceChargingPriceType").focus();
		return;
	} else if (!$("#EffectiveDTTM").val()) {
		$("#EffectiveDTTM").tips({
			side : 2,
			msg : "请选择有效日期",
			bg : '#AE81FF',
			time : 1
		});
		$("#EffectiveDTTM").focus();
		return;
	} 
	var data = {};
	data.BalanceMonth = $("#balanceMonth").val();
	data.BalanceBillName = $("#BalanceBillName").val();
	var balanceChargingPriceType = $("#BalanceChargingPriceType").val();
	data.BalanceChargingPriceType = balanceChargingPriceType;
	if (balanceChargingPriceType == '1') {
		data.ChargingFixedPrice = chargingFixedPriceNameList && chargingFixedPriceNameList[$("#chargingPriceIndex").val()].chargingPrice || 0;
	} else if (balanceChargingPriceType == '2') {
		data.ChargingSharpPrice = chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].sharpChargingPrice || 0;
		data.ChargingPeakPrice = chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].peakChargingPrice || 0;
		data.ChargingFlatPrice = chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].flatChargingPrice || 0;
		data.ChargingValleyPrice = chargingTimePriceNameList && chargingTimePriceNameList[$("#chargingPriceIndex").val()].valleyChargingPrice || 0;
	}
	
	var length = meterInfo.length;
	var chargingStationIDList = [];
	var chargingStationID = undefined;
	for(var i=0; i<length; i++){
		if (chargingStationID != meterInfo[i].chargingStationID) {
			chargingStationID = meterInfo[i].chargingStationID;
			chargingStationIDList.push(meterInfo[i].chargingStationID);
		}
	}
	data.chargingStationID = chargingStationIDList.toString();
	
	var checkMeterLastTimeList = [];
	$("select[id*='CheckMeterLastTime']").each(function(i, item) {
		if (item.id){
			checkMeterLastTimeList.push($("#" + item.id + ' option:selected').text());
		}
	})
	data.checkMeterLastTime = checkMeterLastTimeList.toString();
	
	var checkMeterThisTimeList = [];
	$("select[id*='CheckMeterThisTime']").each(function(i, item) {
		if (item.id){
			checkMeterThisTimeList.push($("#" + item.id + ' option:selected').text());
		}
	})
	data.checkMeterThisTime = checkMeterThisTimeList.toString();
		
	$ajax('saveBillInfo', data, saveBillRecord);
}
function saveBillRecord(data){
	var result = data && data.result;
    if (result == "Success") {
   	 	showMessge(messageContent["Balance.SaveBillSuccess"]);
//   	 	window.parent.skipSecondMenu('costManage/costBalanceBill', 'costBalanceBill');
    } else if (result == "BalanceBillNameExist"){
        showMessge(messageContent["Balance.BalanceBillNameExist"]);
    } else if (result == "DataError"){
        showMessge(messageContent["Balance.BalanceBillDataError"]);
    } else {
   	 	showMessge(messageContent["Balance.SaveBillError"]);
    }
}
function goback() {
	window.parent.skipSecondMenu('costManage/costBalanceBill', 'costBalanceBill');
}